package com.siwei.eqi.controller.sys;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.siwei.eqi.pojo.entity.system.SysRegion;
import com.siwei.eqi.service.sys.SysRegionService;
import com.siwei.eqi.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system")
public class SysRegionController {

    @Autowired
    private SysRegionService sysRegionService;


    @PostMapping
    public void addRedion() {
        try {
            String url = "http://219.153.49.10:7810/sj_assets/v6/api/ecomn/region_boundary?ak=mf1ce8ac07b3ce12dc25a41c3ea4c26339&op=select&key=&reg_type=";
            Map<String, String> params = new HashMap<>();
            params.put("parent_id", "0");
            String s = HttpUtil.get(url, params, new HashMap<>());
            Map<String, Object> parse = (Map<String, Object>) JSON.parse(s);
            Map<String, Object> result = (Map<String, Object>) parse.get("result");
            List<Map<String, String>> itemList = (List<Map<String, String>>) result.get("item_list");
            for (Map<String, String> stringStringMap : itemList) {
                SysRegion province = new SysRegion();
                province.setName(stringStringMap.get("name"));
                province.setCode(Long.valueOf(stringStringMap.get("code")));
                province.setParentCode(0L);
                province.setRegType("省份");
                sysRegionService.addRedion(province);
                params.put("parent_id", stringStringMap.get("code"));
                s = HttpUtil.get(url, params, new HashMap<>());
                Map<String, Object> city = (Map<String, Object>) JSON.parse(s);
                Map<String, Object> citys = (Map<String, Object>) city.get("result");
                if (citys.containsKey("item_list") && citys.get("item_list") instanceof List) {
                    List<Map<String, String>> cityList = (List<Map<String, String>>) citys.get("item_list");
                    for (Map<String, String> stringMap : cityList) {
                        SysRegion region = new SysRegion();
                        region.setCode(Long.valueOf(stringMap.get("code")));
                        region.setName(stringMap.get("name"));
                        region.setProvince(stringStringMap.get("name"));
                        region.setParentCode(Long.valueOf(stringStringMap.get("code")));
                        region.setRegType("地市");
                        sysRegionService.addRedion(region);
                        params.put("parent_id", stringMap.get("code"));
                        String s1 = HttpUtil.get(url, params, new HashMap<>());
                        Map<String, Object> county = (Map<String, Object>) JSON.parse(s1);
                        Map<String, Object> countys = (Map<String, Object>) county.get("result");
                        if (countys.containsKey("item_list") && countys.get("item_list") instanceof List) {
                            List<Map<String, String>> countyList = (List<Map<String, String>>) countys.get("item_list");
                            for (Map<String, String> map : countyList) {
                                SysRegion region1 = new SysRegion();
                                region1.setCode(Long.valueOf(map.get("code")));
                                region1.setName(map.get("name"));
                                region1.setProvince(stringStringMap.get("name"));
                                region1.setParentCode(Long.valueOf(stringMap.get("code")));
                                region1.setCity(stringMap.get("name"));
                                region1.setRegType("区县");
                                sysRegionService.addRedion(region1);
                            }
                        }

                    }
                }
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取新建任务计算区域
     * @param code
     * @return
     */
    @GetMapping("/getRegionList")
    public R getRegionList(String code) {
        return R.ok(sysRegionService.getRegionList(code));
    }
}
